<?php

namespace Admin\Controller;

use Common\Controller\BaseController;

/**
 * app应用安装模块
 */
class AppController extends BaseController {

    public function appList() {
        $pageSize = I('post.pageSize', C('PAGE_SIZE'));
        $pageCurrent = I('post.pageCurrent', 1);
        $orderField = I('post.orderField', 'id');
        $orderDirection = I('post.orderDirection', 'DESC');

        $name = I('post.name', '');

        //参数数组
        $params = array();
        //构造WHERE条件
        $where = '1=1 ';
        if (!empty($name)) {
            $params['name'] = $name;
            $where .= "AND `name` LIKE '%{$name}%' ";
        }

        $app = M('App');
        //数据总数
        $total = $app->where($where)->count();

        //数据列表
        $list = $app->where($where)
                ->order("{$orderField} {$orderDirection}")
                ->page("{$pageCurrent},{$pageSize}")
                ->select();

        if (!empty($list)) {
            $commonStatus = C('COMMON_STATUS');
            foreach ($list as &$item) {
                $item['add_time'] = !empty($item['add_time']) ? date('Y-m-d H:i:s', intval($item['add_time'])) : '';
                $item['status_name'] = array_key_exists($item['status'], $commonStatus) ? $commonStatus[$item['status']] : '';
            }
        }

        $this->assign('pageSize', $pageSize);
        $this->assign('pageCurrent', $pageCurrent);
        $this->assign('orderField', $orderField);
        $this->assign('orderDirection', $orderDirection);
        $this->assign('total', $total);
        $this->assign('list', $list);
        $this->assign('params', $params);
        $this->display();
    }

    public function addApp() {
        if (IS_POST) {
            set_time_limit(0);
            ini_set('post_max_size', '100M');
            ini_set('upload_max_filesize', '100M');

            $name = I('post.name', '');
            $version = I('post.version', '');
            $status = I('post.status', 1);

            if (empty($name)) {
                ajaxReturn('应用名称不能为空', 300);
            }

            if (empty($_FILES['file']['name'])) {
                ajaxReturn('应用文件不能为空', 300);
            }

            $appInfo = uploadOne($_FILES['file'], array('savePath' => 'app/', 'exts' => array('apk', 'ipa', 'jar'), 'maxSize' => 0));
            if (!$appInfo) {
                ajaxReturn('上传应用文件到本地失败', 300);
            } else {
                $appFilePath = C('UPLOAD_CONFIG.rootPath') . $appInfo['savepath'] . $appInfo['savename'];
                if (aliossUploadOne($appFilePath)) {
                    $appOssUrl = getImgUrl($appFilePath);

                    $qrFile = createQR($appOssUrl);

                    if ($qrFile === false) {
                        ajaxReturn('生成本地二维码失败', 300);
                    }

                    if (aliossUploadOne($qrFile)) {
                        $ret = M('App')->add(array(
                            'name' => $name,
                            'version' => $version,
                            'local_url' => $appFilePath,
                            'remote_url' => $appOssUrl,
                            'qr_local_url' => $qrFile,
                            'qr_remote_url' => getImgUrl($qrFile),
                            'add_time' => time(),
                            'upd_time' => time(),
                            'status' => $status,
                        ));

                        if ($ret) {
                            ajaxReturn('应用添加成功', 200, '', '', '', 'Admin/App/appList');
                        } else {
                            ajaxReturn('应用添加失败', 300);
                        }
                    } else {
                        ajaxReturn('上传二维码到阿里云OSS失败', 300);
                    }
                } else {
                    ajaxReturn('上传应用文件到阿里云失败', 300);
                }
            }
        } else {
            $commonStatus = C('COMMON_STATUS');

            $this->assign('commonStatus', $commonStatus);
            $this->display();
        }
    }

    public function updApp() {
        if (IS_POST) {
            set_time_limit(0);
            ini_set('post_max_size', '100M');
            ini_set('upload_max_filesize', '100M');
            
            $appId = I('post.appId', 0);
            $name = I('post.name', '');
            $version = I('post.version', '');
            $status = I('post.status', 1);

            if (empty($name)) {
                ajaxReturn('应用名称不能为空', 300);
            }

            $data = array(
                'name' => $name,
                'version' => $version,
                'upd_time' => time(),
                'status' => $status,
            );
            if (!empty($_FILES['file']['name'])) {
                $appInfo = uploadOne($_FILES['file'], array('savePath' => 'app/', 'exts' => array('apk', 'ipa', 'jar'), 'maxSize' => 0));
                if (!$appInfo) {
                    ajaxReturn('上传应用文件到本地失败', 300);
                } else {
                    $appFilePath = C('UPLOAD_CONFIG.rootPath') . $appInfo['savepath'] . $appInfo['savename'];
                    if (aliossUploadOne($appFilePath)) {
                        $appOssUrl = getImgUrl($appFilePath);

                        $qrFile = createQR($appOssUrl);

                        if ($qrFile === false) {
                            ajaxReturn('生成本地二维码失败', 300);
                        }

                        if (aliossUploadOne($qrFile)) {
                            $data['local_url'] = $appFilePath;
                            $data['remote_url'] = $appOssUrl;
                            $data['qr_local_url'] = $qrFile;
                            $data['qr_remote_url'] = getImgUrl($qrFile);
                        } else {
                            ajaxReturn('上传二维码到阿里云OSS失败', 300);
                        }
                    } else {
                        ajaxReturn('上传应用文件到阿里云失败', 300);
                    }
                }
            }

            $ret = M('App')->where("id={$appId}")->save($data);

            if ($ret !== false) {
                ajaxReturn('更新成功', 200, '', '', '', 'Admin/App/appList');
            } else {
                ajaxReturn('更新失败', 300);
            }
        } else {
            $appId = I('get.appId', 0);

            if (empty($appId)) {
                ajaxReturn('应用ID不能为空', 300);
            }

            $appInfo = M('App')->where("id={$appId}")->find();
            if (empty($appInfo)) {
                ajaxReturn('没有找到该应用信息', 300);
            }

            $commonStatus = C('COMMON_STATUS');

            $this->assign('appInfo', $appInfo);
            $this->assign('commonStatus', $commonStatus);
            $this->display();
        }
    }

    public function delApp() {
        $ids = I('get.ids', '');

        if (empty($ids)) {
            ajaxReturn('请选择删除项', 300);
        }

        $ret = M('App')->where("FIND_IN_SET(id,'{$ids}')")->delete();

        if ($ret !== false) {
            ajaxReturn('删除成功', 200, '', '', '', 'Admin/App/appList');
        } else {
            ajaxReturn('删除失败', 300);
        }
    }

}
